package cn.calm.repaymentNotice.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import cn.calm.repaymentNotice.R;
import cn.calm.repaymentNotice.client.RepaymentNoticeException;
import cn.calm.repaymentNotice.entity.RepaymentNotice;
import cn.calm.repaymentNotice.entity.RepaymentNoticeInfo;
import cn.calm.repaymentNotice.util.NoticeInfoUtil;

public class RepaymentNoticeAdapter extends BaseAdapter {
	
	private List<RepaymentNoticeInfo> notices ;
	
	private LayoutInflater inflater;
	
	private int resourceId;
	
	public RepaymentNoticeAdapter(List<RepaymentNoticeInfo> notices, Context context,
			int resourceId) {
		this.notices = notices;
		inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.resourceId = resourceId;
	}
	
	public int getCount() {
		return notices.size();
	}

	public Object getItem(int position) {
		return notices.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView==null){
			convertView=inflater.inflate(resourceId, null);
		}
		RepaymentNoticeInfo notice=notices.get(position);
		try {
			info(convertView,notice);
		} catch (RepaymentNoticeException e) {
			e.printStackTrace();
		}
		return convertView;
	}
	
	private void  info(View convertView,RepaymentNoticeInfo notice) throws RepaymentNoticeException{
		TextView name = (TextView) convertView.findViewById(R.id.name);
		if(notice.noticeType==RepaymentNotice.TYPE_CREDIT_CARD){
			name.setText("您的信用卡:"+notice.name);
		}else{
			name.setText("您的贷款:"+notice.name);
		}
		
		TextView info=(TextView) convertView.findViewById(R.id.info);
		info.setText(NoticeInfoUtil.info(notice.predictRepaymentDate));
	}
}
